package com.dlc.shop.sms4j.config;


import com.dlc.shop.common.bean.Sms4jConfig;
import com.dlc.shop.common.bean.SysServiceConfig;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.util.Json;
import com.dlc.shop.common.util.RedisUtil;
import com.dlc.shop.service.SysConfigService;
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
import org.dromara.sms4j.cloopen.config.CloopenConfig;
import org.dromara.sms4j.core.datainterface.SmsReadConfig;
import org.dromara.sms4j.ctyun.config.CtyunConfig;
import org.dromara.sms4j.dingzhong.config.DingZhongConfig;
import org.dromara.sms4j.emay.config.EmayConfig;
import org.dromara.sms4j.huawei.config.HuaweiConfig;
import org.dromara.sms4j.jdcloud.config.JdCloudConfig;
import org.dromara.sms4j.lianlu.config.LianLuConfig;
import org.dromara.sms4j.netease.config.NeteaseConfig;
import org.dromara.sms4j.provider.config.BaseConfig;
import org.dromara.sms4j.qiniu.config.QiNiuConfig;
import org.dromara.sms4j.tencent.config.TencentConfig;
import org.dromara.sms4j.unisms.config.UniConfig;
import org.dromara.sms4j.yunpian.config.YunpianConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author admin
 */
@Component
public class ReadConfig implements SmsReadConfig {

    @Autowired
    private SysConfigService sysConfigService;

    @Override
    public BaseConfig getSupplierConfig(String config) {
        BaseConfig baseConfig = null;
        Object paramValue = RedisUtil.get(Constant.SMS_CONFIG  + Constant.COLON);
        if (Objects.isNull(paramValue)) {
            return baseConfig;
        }
        Sms4jConfig sms4jConfig = Json.parseObject(paramValue.toString(), Sms4jConfig.class);
        if (Objects.isNull(config)) {
            config = sms4jConfig.getSmsType();
        }
        baseConfig = handSmsConfig(config, sms4jConfig);
        return baseConfig;
    }

    @Override
    public List<BaseConfig> getSupplierConfigList() {
        List<BaseConfig> baseConfigs = new ArrayList<>();
        Object paramValue = RedisUtil.get(Constant.SMS_CONFIG  + Constant.COLON);
        if (Objects.isNull(paramValue)) {
            paramValue = sysConfigService.getValue(Constant.SMS_CONFIG);
            if (Objects.isNull(paramValue)) {
                return baseConfigs;
            }
        }
        Sms4jConfig sms4jConfig = Json.parseObject(paramValue.toString(), Sms4jConfig.class);
        BaseConfig baseConfig = handSmsConfig(sms4jConfig.getSmsType(), sms4jConfig);
        baseConfigs.add(baseConfig);
        return baseConfigs;
    }

    /***
     * 处理短信类型
     * @param config
     * @param sms4jConfig
     */
    private BaseConfig handSmsConfig(String config, Sms4jConfig sms4jConfig) {
        BaseConfig baseConfig = null;
        switch (config) {
            case "unisms" :
                UniConfig uniConfig = new UniConfig();
                baseConfig = uniConfig;
                break;
            case "yunpian" :
                YunpianConfig yunpianConfig = new YunpianConfig();
                yunpianConfig.setCallbackUrl(sms4jConfig.getCallbackUrl());
                baseConfig = yunpianConfig;
                break;
            case "ctyun" :
                CtyunConfig ctyunConfig = new CtyunConfig();
                baseConfig = ctyunConfig;
                break;
            case "netease" :
                NeteaseConfig neteaseConfig = new NeteaseConfig();
                baseConfig = neteaseConfig;
                break;
            case "alibaba" :
                AlibabaConfig alibabaConfig = new AlibabaConfig();
                baseConfig = alibabaConfig;
                break;
            case "tencent" :
                TencentConfig tencentConfig = new TencentConfig();
                baseConfig = tencentConfig;
                break;
            case "huawei" :
                HuaweiConfig huaweiConfig = new HuaweiConfig();
                huaweiConfig.setSender(sms4jConfig.getSender());
                huaweiConfig.setStatusCallBack(sms4jConfig.getStatusCallBack());
                huaweiConfig.setUrl(sms4jConfig.getUrl());
                baseConfig = huaweiConfig;
                break;
            case "jdcloud" :
                JdCloudConfig jdCloudConfig = new JdCloudConfig();
                baseConfig = jdCloudConfig;
                break;
            case "cloopen" :
                CloopenConfig cloopenConfig = new CloopenConfig();
                baseConfig = cloopenConfig;
                break;
            case "emay" :
                EmayConfig emayConfig = new EmayConfig();
                emayConfig.setRequestUrl(sms4jConfig.getRequestUrl());
                baseConfig = emayConfig;
                break;
            case "lianlu" :
                LianLuConfig lianLuConfig = new LianLuConfig();
                lianLuConfig.setMchId(sms4jConfig.getMchId());
                lianLuConfig.setAppId(sms4jConfig.getAppId());
                lianLuConfig.setAppKey(sms4jConfig.getAppKey());
                baseConfig = lianLuConfig;
                break;
            case "dingzhong" :
                DingZhongConfig dingZhongConfig = new DingZhongConfig();
                baseConfig = dingZhongConfig;
                break;
            case "qiniu" :
                QiNiuConfig qiNiuConfig = new QiNiuConfig();
                qiNiuConfig.setSignatureId(sms4jConfig.getSignatureId());
                baseConfig = qiNiuConfig;
                break;
            default:
                // 默认使用阿里云短信
                baseConfig = new AlibabaConfig();
                break;
        }
        baseConfig.setConfigId(config);
        baseConfig.setAccessKeyId(sms4jConfig.getAccessKeyId());
        baseConfig.setAccessKeySecret(sms4jConfig.getAccessKeySecret());
        baseConfig.setSignature(sms4jConfig.getSignName());
        return baseConfig;
    }
}

